/**
 * Copyright (C) 2008-2009 Cristian Olaru <colaru@gmail.com>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *         http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.integrationpath.mengine.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import com.integrationpath.mengine.dao.ArticleDao;
import com.integrationpath.mengine.model.Article;
import com.integrationpath.mengine.model.Tag;
import com.integrationpath.mengine.service.ArticleManager;

@WebService(endpointInterface = "com.integrationpath.mengine.service.ArticleManager")
public class ArticleManagerImpl implements ArticleManager {
    private ArticleDao articleDao;

	public List<Article> getArticlesByCategoryName(String categoryName) {
		return articleDao.getArticlesByCategoryName(categoryName);
	}

	public List<Article> getArticlesByTagName(String name) {
		return articleDao.getArticlesByTagName(name);
	}

	public ArticleDao getArticleDao() {
		return articleDao;
	}

	public void setArticleDao(ArticleDao articleDao) {
		this.articleDao = articleDao;
	}

	public List<Article> getArticlesByMediaType(int mediaId) {
		return articleDao.getArticlesByMediaType(mediaId);
	}

	public boolean isArticleContainingPictures(Long articleId) {
		List articleList = articleDao.isArticleContainingPictures(articleId);
		int articleListSize = 0;
		if(articleList != null)
			articleListSize = articleList.size();
		return articleListSize>0;
	}

	public List<Article> getArticlesByType(int typeId) {
		return articleDao.getArticlesByType(typeId);
	}

	public List<Article> getArticlesBySize(int size) {
		return articleDao.getArticlesBySize(size);
	}

	public List<Article> getArticlesByVotings(int size) {
		return articleDao.getArticlesByVotings(size);
	}

	public List<Article> getArticlesByTypeAndSize(int typeId, int size) {
		return articleDao.getArticlesByTypeAndSize(typeId, size);
	}

	public List<Article> getArticlesByUser(long userId) {
		return articleDao.getArticlesByUser(userId);
	}

	public List<Article> getArticlesByDate(int year, int month) {
		return articleDao.getArticlesByDate(year, month);
	}

	public Map getMonthsAndYears() {
		Map lines = new HashMap();
		Calendar calendar = Calendar.getInstance();
		String monthName = "";
		Date current;
		int realMonth = 0 ;
		List dates = articleDao.getMonthsAndYears();
		for (Object object : dates) {
			current = (Date) object;
			calendar.setTime(current);
			
			switch (calendar.get(Calendar.MONTH)) {
	            case 0:  monthName = "January"; break;
	            case 1:  monthName = "February"; break;
	            case 2:  monthName = "March"; break;
	            case 3:  monthName = "April"; break;
	            case 4:  monthName = "May"; break;
	            case 5:  monthName = "June"; break;
	            case 6:  monthName = "July"; break;
	            case 7:  monthName = "August"; break;
	            case 8:  monthName = "September"; break;
	            case 9: monthName = "October"; break;
	            case 10: monthName = "November"; break;
	            case 11: monthName = "December"; break;
	            default: monthName = "Invalid month."; break;
		  }
			realMonth = calendar.get(Calendar.MONTH) + 1;
			lines.put("aMonth=" + realMonth + "&aYear=" + calendar.get(Calendar.YEAR), monthName  + " " + calendar.get(Calendar.YEAR));	
		}
		
		return lines;
	}

	public byte[] getArticleThumbnail(Long id) {
		return articleDao.getArticleThumbnail(id);
	}

	public List getFeaturedArticles() {
		return articleDao.getFeaturedArticles();
	}  
}
